import * as THREE from 'three';
import { Vec3 } from '@/core/types';
import { PassiveEntity } from './PassiveEntity';
import { EntityType, EntityStats } from './types';
import { WorldManager } from '@/world/WorldManager';
import { ItemDropManager } from '@/world/ItemDrop';
import { BlockType } from '@/blocks/types';
import { getAudioManager } from '@/audio/AudioManager';

export class Pig extends PassiveEntity {
  constructor(id: string, position: Vec3, worldManager: WorldManager, itemDropManager?: ItemDropManager) {
    const stats: EntityStats = {
      maxHealth: 10,
      moveSpeed: 1.8,
      jumpHeight: 1.0,
      detectionRange: 6,
      size: { width: 1.2, height: 0.9 }
    };

    super(id, EntityType.PIG, position, worldManager, stats, itemDropManager);
  }

  protected createMesh(): void {
    // 创建一个简单的猪模型
    const group = new THREE.Group();

    // 身体
    const bodyGeometry = new THREE.BoxGeometry(1.2, 0.6, 0.8);
    const bodyMaterial = new THREE.MeshLambertMaterial({ color: 0xFFC0CB }); // 粉色
    const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
    body.position.set(0, 0.3, 0);
    group.add(body);

    // 头部
    const headGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.6);
    const headMaterial = new THREE.MeshLambertMaterial({ color: 0xFFB6C1 }); // 稍浅的粉色
    const head = new THREE.Mesh(headGeometry, headMaterial);
    head.position.set(0, 0.5, -0.6);
    group.add(head);

    // 猪鼻子
    const snoutGeometry = new THREE.BoxGeometry(0.3, 0.2, 0.2);
    const snoutMaterial = new THREE.MeshLambertMaterial({ color: 0xFF69B4 }); // 深粉色
    const snout = new THREE.Mesh(snoutGeometry, snoutMaterial);
    snout.position.set(0, 0.5, -0.9);
    group.add(snout);

    // 腿部
    const legGeometry = new THREE.BoxGeometry(0.15, 0.4, 0.15);
    const legMaterial = new THREE.MeshLambertMaterial({ color: 0xFFB6C1 });
    
    const positions = [
      [-0.35, -0.2, -0.25], // 前左腿
      [0.35, -0.2, -0.25],  // 前右腿
      [-0.35, -0.2, 0.25],  // 后左腿
      [0.35, -0.2, 0.25]    // 后右腿
    ];

    positions.forEach(pos => {
      const leg = new THREE.Mesh(legGeometry, legMaterial);
      leg.position.set(pos[0], pos[1], pos[2]);
      group.add(leg);
    });

    // 尾巴
    const tailGeometry = new THREE.CylinderGeometry(0.05, 0.05, 0.3);
    const tailMaterial = new THREE.MeshLambertMaterial({ color: 0xFFB6C1 });
    const tail = new THREE.Mesh(tailGeometry, tailMaterial);
    tail.position.set(0, 0.4, 0.5);
    tail.rotation.x = Math.PI / 4; // 稍微向上翘
    group.add(tail);

    // 耳朵
    const earGeometry = new THREE.ConeGeometry(0.1, 0.2, 4);
    const earMaterial = new THREE.MeshLambertMaterial({ color: 0xFF69B4 });
    
    const leftEar = new THREE.Mesh(earGeometry, earMaterial);
    leftEar.position.set(-0.15, 0.7, -0.6);
    leftEar.rotation.z = -Math.PI / 6;
    group.add(leftEar);
    
    const rightEar = new THREE.Mesh(earGeometry, earMaterial);
    rightEar.position.set(0.15, 0.7, -0.6);
    rightEar.rotation.z = Math.PI / 6;
    group.add(rightEar);

    // 设置阴影
    group.traverse((child) => {
      if (child instanceof THREE.Mesh) {
        child.castShadow = true;
        child.receiveShadow = true;
      }
    });

    this.mesh = group;
  }

  protected dropItems(): void {
    console.log(`Pig ${this.id} dropped raw porkchop`);
    
    if (this.itemDropManager) {
      // 掉落生猪肉 (1-3个)
      const porkchopCount = 1 + Math.floor(Math.random() * 3);
      this.itemDropManager.dropItem(BlockType.RAW_PORKCHOP, porkchopCount, this.position);
    }
  }

  public update(deltaTime: number): void {
    super.update(deltaTime);
    
    // 添加猪特有的行为
    this.addPigBehavior(deltaTime);
  }

  private addPigBehavior(deltaTime: number): void {
    // 偶尔发出猪叫声
    if (Math.random() < 0.0008) {
      console.log(`Pig ${this.id}: Oink!`);
      this.playIdleSound();
    }

    // 尾巴摆动动画
    if (this.mesh) {
      const tail = this.mesh.children[5]; // 尾巴是第六个子对象
      if (tail) {
        tail.rotation.y = Math.sin(this.age * 3) * 0.2;
      }
    }

    // 猪比较活跃，移动速度稍快
    if (this.ai?.state === 'wandering') {
      this.stats.moveSpeed = 2.0;
    } else {
      this.stats.moveSpeed = 1.8;
    }
  }

  private playIdleSound(): void {
    const audioManager = getAudioManager();
    audioManager.playSound('pig_idle', {
      position: this.position,
      volume: 0.5
    });
  }

  // 猪可以被胡萝卜吸引
  public canBeAttractedBy(item: string): boolean {
    return item === 'carrot' || item === 'potato';
  }
}